//
//  XZModifyPhoneVC.m
//  Teecloud
//
//  Created by PC on 2018/11/12.
//  Copyright © 2018年 小志. All rights reserved.
//
#import <sys/utsname.h>
#import "XZModifyPhoneVC.h"
#import "JKCountDownButton.h"
#import "DeviceInfoManager.h"
@interface XZModifyPhoneVC ()
@property (weak, nonatomic) IBOutlet UITextField *reasonTf;
@property (weak, nonatomic) IBOutlet UITextField *codeTf;
@property (weak, nonatomic) IBOutlet UITextField *phoneTf;
@property (weak, nonatomic) IBOutlet UILabel *currentModel;

@end

@implementation XZModifyPhoneVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"申请变更绑定手机型号";
    self.view.backgroundColor = kWhiteColor;
    //当前型号
    const NSString *mobileModel = [[DeviceInfoManager sharedManager] getDeviceName];
    self.currentModel.text = [NSString stringWithFormat:@"%@",mobileModel];
}
//获取手机验证码
- (IBAction)messageButtonAction:(JKCountDownButton *)sender {
    if (kIsEmptyString(self.phoneTf.text)) {
        [MBProgressHUD showErrorOrSuc:@"请输入手机号" toView:self.view];
        return;
    }
    if (![self.phoneTf.text zj_isTrueMobilePhone]) {
        [MBProgressHUD showErrorOrSuc:@"请输入正确的手机号" toView:self.view];
        return;
    }
    
    kWeakObject(self)
    NSDictionary *dict= @{
                          @"from":FROM_IOS,
                          @"busiOrgId":[XGMArchiverTools cacheForKey:BUSIORGID],
                          @"loginFinger":[XGMArchiverTools cacheForKey:USER_identifierNumber],
                          @"userId":[XGMArchiverTools cacheForKey:USER_UserId],
                          @"mobileCode":self.phoneTf.text,
                          @"temp":@"3"
                          };
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [HJNetwork POSTWithURL:[NSString stringWithFormat:@"/do-api.ashx?$m=SendVerifyCode&$v=%@",VERSION] parameters:dict cachePolicy:HJCachePolicyIgnoreCache callback:^(id responseObject, NSError *error) {
        [MBProgressHUD hideHUDForView:weakself.view animated:YES];
        if (!error) {
            if ([[responseObject objectForKey:RESULT_NAME_Success] boolValue]) {
                NSLog(@"%@",responseObject);
                [MBProgressHUD showErrorOrSuc:@"验证码发送成功" toView:weakself.view];
                //验证码倒计时
                sender.enabled = NO;
                [sender startCountDownWithSecond:60];
                sender.backgroundColor =  kDarkGrayColor;
                [sender countDownChanging:^NSString *(JKCountDownButton *countDownButton,NSUInteger second) {
                    NSString *title = [NSString stringWithFormat:@"%zd秒后重新获取验证码",second];
                    return title;
                }];
                [sender countDownFinished:^NSString *(JKCountDownButton *countDownButton, NSUInteger second) {
                    countDownButton.backgroundColor = kGreenColor;
                    countDownButton.enabled = YES;
                    return @"获取短信验证码";
                }];
                
            }else{
                /**显示错误信息*/
                [MBProgressHUD showErrorOrSuc:[responseObject objectForKey:RESULT_NAME_Message] toView:weakself.view];
            }
        }else{
            [MBProgressHUD showErrorOrSuc:[NSString stringWithFormat:@"%@",error] toView:weakself.view];
        }
    }];
    
}

//提交变更申请
- (IBAction)submitButtonAction:(UIButton *)sender {
    if (kIsEmptyString(self.reasonTf.text)) {
        [MBProgressHUD showErrorOrSuc:@"请输入变更原因" toView:self.view];
        return;
    }
    if (kIsEmptyString(self.phoneTf.text)) {
        [MBProgressHUD showErrorOrSuc:@"请输入手机号" toView:self.view];
        return;
    }
    if (![self.phoneTf.text zj_isTrueMobilePhone]) {
        [MBProgressHUD showErrorOrSuc:@"请输入正确的手机号" toView:self.view];
        return;
    }
    
    if (kIsEmptyString(self.codeTf.text)) {
        [MBProgressHUD showErrorOrSuc:@"请输入短信验证码" toView:self.view];
        return;
    }
    kWeakObject(self)
    const NSString *mobileModel = [[DeviceInfoManager sharedManager] getDeviceName];
    NSDictionary *dict= @{
                          @"from":FROM_IOS,
                          @"busiOrgId":[XGMArchiverTools cacheForKey:BUSIORGID],
                          @"loginFinger":[XGMArchiverTools cacheForKey:USER_identifierNumber],
                          @"userId":[XGMArchiverTools cacheForKey:USER_UserId],
                          @"mobileCode":self.phoneTf.text,
                          @"verifyCode":self.codeTf.text,
                          @"reason":self.reasonTf.text,
                          @"mobileModel":mobileModel
                          };
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [HJNetwork POSTWithURL:[NSString stringWithFormat:@"ajax-api.ashx?$c=TeeyunApp&$m=PostMobileModelApply&$v=%@",VERSION] parameters:dict cachePolicy:HJCachePolicyIgnoreCache callback:^(id responseObject, NSError *error) {
        [MBProgressHUD hideHUDForView:weakself.view animated:YES];
        if (!error) {
            if ([[responseObject objectForKey:RESULT_NAME_Success] boolValue]) {
                //                NSString *msg = [responseObject objectForKey:RESULT_NAME_Message];
                //                if (msg) {
                //                    [MBProgressHUD showErrorOrSuc:msg toView:weakself.view];
                //                }else{
                //                    [MBProgressHUD showErrorOrSuc:@"" toView:weakself.view];
                //                }
                dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC);
                dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
                    [self.navigationController popViewControllerAnimated:true];
                });
            }else{
                /**显示错误信息*/
                [MBProgressHUD showErrorOrSuc:[responseObject objectForKey:RESULT_NAME_Message] toView:weakself.view];
            }
        }else{
            [MBProgressHUD showErrorOrSuc:[NSString stringWithFormat:@"%@",error] toView:weakself.view];
        }
    }];
}


@end
